/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: AbstractAlbarethGameState.java 46 2008-05-15 18:09:39Z jens464 $
*/
package albareth.frontend.slick.state;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;
import albareth.frontend.slick.ui.UIElement;
import albareth.frontend.slick.ui.UIManager;

public abstract class AbstractAlbarethGameState extends BasicGameState
{
    protected SlickFrontend _frontend;
    private UIManager _uiManager;
    
    public AbstractAlbarethGameState(SlickFrontend frontend)
    {
        _frontend = frontend;
    }
    
    public final void addUIElement(UIElement elem)
    {
        _uiManager.addChild(elem);
    }
    
    public final boolean removeUIElement(UIElement elem)
    {
        return _uiManager.removeChild(elem);
    }
    
    public final void removeAllUIElements()
    {
        _uiManager.removeAllChildren();
    }
    
    public final void setBackgroundImage(Image image)
    {
        _uiManager.setBackgroundImage(image);
    }

    public final void enter(GameContainer container, StateBasedGame game)
            throws SlickException
    {
        enter();
    }

    public void leave(GameContainer container, StateBasedGame game)
        throws SlickException
    {
        leave();
    }

    

    public int getID()
    {
        // TODO Auto-generated method stub
        return 0;
    }

    public final void init(GameContainer container, StateBasedGame game)
            throws SlickException
    {
        _uiManager = new UIManager(container, SlickResources.DEFAULT_FONT);
        init();
    }
    
    public final void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException
    {
        render(g);
        _uiManager.renderUI(g, container);

    }
    
    public final void update(GameContainer container, StateBasedGame game, int delta)
            throws SlickException
    {
        update(delta);
        _uiManager.updateUI(delta, container);
    }
    
    public final void mouseMoved(int oldx, int oldy, int newx, int newy)
    {
        boolean consumed = _uiManager.processMouseMoved(newx, newy);
        if (!consumed) {
            mouseMovedFallthrough(newx, newy);
        }
    }


    public final void mousePressed(int button, int x, int y)
    {
        boolean consumed = _uiManager.processMousePressed(button, x, y);
        if (!consumed) {
            mouseDownFallthrough(button, x, y);
        }
    }
    
    public final void mouseReleased(int button, int x, int y)
    {
        boolean consumed = _uiManager.processMouseReleased(button, x, y);
        if (!consumed) {
            mouseUpFallthrough(button, x, y);
        }
    }


    public final void keyPressed(int key, char c)
    {
        boolean consumed = _uiManager.processKeyPressed(key, c);
        if (!consumed) {
            keyPressedFallthrough(key, c);
        }
    }

    public final void keyReleased(int key, char c)
    {
        boolean consumed = _uiManager.processKeyReleased(key, c);
        if (!consumed) {
            keyReleasedFallthrough(key, c);
        }
    }

    
    protected abstract void enter() throws SlickException;
    
    protected abstract void leave() throws SlickException;

    protected abstract void init() throws SlickException;

    protected abstract void render(Graphics g) throws SlickException;

    protected abstract void update(int delta) throws SlickException;

    
    
    
    
    
    protected void keyPressedFallthrough(int key, char c)
    {
    }

    protected void keyReleasedFallthrough(int key, char c)
    {
    }

    protected void mouseMovedFallthrough(int newx, int newy)
    {
    }
    
    protected void mouseDownFallthrough(int button, int x, int y)
    {
    }

    protected void mouseUpFallthrough(int button, int x, int y)
    {
    }






}
